<template>
  <div>
    <div class="table-title">
      <slot name="tableheader"></slot>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column v-if="showTableSelection" type="selection" width="60">
      </el-table-column>
      <el-table-column
        v-if="showTableIndex"
        type="index"
        label="序号"
        width="60"
      >
      </el-table-column>
      <template v-for="item in propsList" :key="item.prop">
        <el-table-column
          :prop="item.prop"
          :label="item.label"
          :width="item.width || '180'"
        >
          <!-- 表格自定义内容 template -->
          <template #default="scoped">
            <!-- 插入自定义具名插槽  插槽上传入任何属性都能在外部具体的插槽内获取到 -->
            <slot :name="item.slotName || item.prop" :row="scoped.row">{{
              scoped.row[item.prop]
            }}</slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <el-pagination
      :currentPage="currentPage"
      @update:currentPage="currentPage = $event"
      :page-size="pageSize"
      @update:page-size="pageSize = $event"
      :page-sizes="[5, 10, 20, 30, 40]"
      :small="false"
      :disabled="false"
      :background="false"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageOption.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, ref } from 'vue';

export default defineComponent({
  name: 'XMTable',
  props: {
    tableData: {
      type: Array,
      required: true,
    },
    propsList: {
      type: Array as PropType<any[]>,
      required: true,
    },
    showTableIndex: {
      type: Boolean,
      default: false,
    },
    showTableSelection: {
      type: Boolean,
      default: false,
    },
    pageOption: {
      type: Object,
      default: () => ({
        total: 10,
        size: 5,
        current: 1,
      }),
    },
  },
  emits: ['changePageSize', 'changePageCurrent'],
  setup(props, { emit }) {
    const currentPage = ref(props.pageOption.current);
    const pageSize = ref(props.pageOption.size);
    const total = ref(props.pageOption.total);
    const handleSizeChange = (size: string) => {
      // console.log('handleSizeChange', size);
      pageSize.value = size;
      emit('changePageSize', size);
    };
    const handleCurrentChange = (current: string) => {
      // console.log('handleCurrentChange', current);
      currentPage.value = current;
      emit('changePageCurrent', current);
    };

    return {
      currentPage,
      pageSize,
      total,
      handleSizeChange,
      handleCurrentChange,
      // changeCurrentPage,
      // changePageSize,
    };
  },
});
</script>

<style scoped></style>
